package Java7_Concurrency_Cookbook.thread_synchronized_base.condition;

/**
 * 生产者</br>
 * 从 FileMock 中不停的读取内容插入到 Buffer
 * @author lixiangke
 */
public class Producer implements Runnable {

    FileMock mock;
    Buffer buffer;

    public Producer(FileMock mock, Buffer buffer) {
        this.mock = mock;
        this.buffer = buffer;
    }

    @Override
    public void run() {
        buffer.setPendingLines(true);
        while (mock.hasMoreLines()) {
            String line = mock.getLine();
            buffer.insert(line);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        buffer.setPendingLines(false);
    }
}
